import QtQuick 2.12
import QtQuick.Controls 1.4
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.12
import QtLocation 5.12
import QtQuick.Window 2.12
import QtGraphicalEffects 1.0
import AIClient.module 1.0
import Result.module 1.0
import QtQuick.Dialogs 1.3
//

ApplicationWindow {

    id: window
    visible: true
    //visibility: "Maximized"
    minimumHeight: Screen.desktopAvailableHeight*0.5
//    maximumHeight: 960
    minimumWidth: Screen.desktopAvailableWidth*0.5
//    maximumWidth: 1600
    width: Screen.desktopAvailableWidth*0.8
    height: Screen.desktopAvailableHeight*0.8
//    width: 1600
//    height: 960
    title: qsTr("Sugar")

    property bool m_blog:false
    property string m_token: ""
    property string m_email: ""
    TipMessage {
        id: tip
        width: parent.width
    }

    AIClient {
        id: client
    }


    PasswordForgetReset {
        id: password_forget_page

    }

    Register {
        id: register_page
    }

    ResetPassword {
        id: password_reset_page
    }




    Window{
        modality: Qt.ApplicationModal
        title: "登录"
        id: login_dlg
        visible: false

        height: Screen.desktopAvailableHeight*0.3
        width: Screen.desktopAvailableWidth*0.3

        function reset()
        {
            filedPawd.text = ""
        }
            Image {
                anchors.fill: parent
                source: "qrc:/images/images/AIback.jpg"
            }
            TipMessage {
                id: tip_log
                width: parent.width
            }
//            Item {
//                width: 650
//                height: 650
//                anchors.centerIn: parent
//                //一个圆形图片
//                Item {
//                    width: parent.width
//                    height: parent.height
//                    Image {
//                        id: rdJpg
//                        anchors.centerIn: parent
//                        smooth: true
//                        visible: false
//                        source: "qrc:/images/AIback.jpg"
//                    }
//                    Rectangle {
//                        id: mask
//                        width: parent.width
//                        height: parent.height
//                        radius: width/2
//                        visible: false
//                    }
//                    OpacityMask {
//                        anchors.fill: parent
//                        source: rdJpg
//                        maskSource: mask
//                    }
//                }

                //用户输入
                Rectangle {
                    width: 250
                    height: 180
                    color: "transparent"
                    anchors.centerIn: parent

//                    anchors.bottom: parent.bottom
//                    anchors.bottomMargin: 100
//                    anchors.horizontalCenter: parent.horizontalCenter

                    GridLayout {
                        id: grid
                        rows:3
                        columns: 3
                        anchors.fill: parent
                        columnSpacing: 10
                        Text {
                            Layout.column: 0
                            Layout.row: 0
                            text: qsTr("邮箱")
                            font.pointSize: 12
                            font.bold: true
                            Layout.minimumHeight: 20
                            Layout.maximumHeight: 50
                            color: "orange"
                        }
                        Rectangle {
                            color: "white"
                            Layout.column: 1
                            Layout.row: 0
                            Layout.minimumHeight: 20
                            Layout.maximumHeight: 60
                            Layout.preferredWidth: 200
                            Layout.preferredHeight: 50
                            TextField {
                                id: filedUsr
//                                font.pointSize: 12
                                anchors.fill: parent
                                text: "superadmin@aiplatform.com"

                                selectByMouse: true
                                background: Rectangle{
                                    color: "white"
                                    anchors.fill: filedUsr
                                }
                            }
                        }
                        Text {
                            Layout.column: 0
                            Layout.row: 1
                            text: qsTr("密码")
                            Layout.minimumHeight: 20
                            Layout.maximumHeight: 50
                            font.pointSize: 12
                            font.bold: true
                            color: "orange"
                        }

                        TextField {
                            Layout.column: 1
                            Layout.row: 1
                            id: filedPawd
                            font.pointSize: 12
                            Layout.minimumHeight: 20
                            Layout.maximumHeight: 60
                            Layout.preferredWidth: 200
                            echoMode: "Password"
                            passwordCharacter: "*"
                            text: "superadmin"
                            Layout.preferredHeight: 50
                            selectByMouse: true
                            background: Rectangle{
                                color: "white"
                                anchors.fill: filedPawd
                            }
                        }

                        Button {
                            Layout.column: 2
                            Layout.row: 1
                            id: btn_forget_passwd
                            text:"忘记密码"
                            font.underline: true
                            font.bold: btn_forget_passwd.hovered
                            contentItem: Text {
                                text: btn_forget_passwd.text
                                font: btn_forget_passwd.font
                                opacity: enabled ? 1.0 : 0.3
                                color: "orange"
                                horizontalAlignment: Text.AlignHCenter
                                verticalAlignment: Text.AlignVCenter
                                elide: Text.ElideRight
                            }
                            background: Rectangle{
                                anchors.fill: btn_forget_passwd
                                color: "transparent"
//                                Text {
//                                    color: "orange"
//                                    text: qsTr("忘记密码")
//                                    font.bold: btn_forget_passwd.hovered
//                                    anchors.fill: parent
//                                }
                            }
                            onClicked: {
                                password_forget_page.reset(filedUsr.text)
                                password_forget_page.show()
                            }
                        }

                        //登录按钮
                        Rectangle {
                            id: logBtn
                            width: 150
                            height: 33
                            color: "#6facf9"
                            radius: 4
                            border.width: 1
                            border.color: "black"
                            Layout.column: 1
                            Layout.row: 2
                            Layout.fillWidth: true
                            Text {
                                color: "#ffffff"
                                text: qsTr("登录")
                                anchors.centerIn: parent
                                font.pointSize: 12
                            }
                            Layout.alignment: Qt.AlignVCenter | Qt.AlignHCenter
//                            anchors.horizontalCenter: parent.horizontalCenter
//                            anchors.top: grid.bottom
//                            anchors.topMargin: 40

                            MouseArea {
                                anchors.fill: parent
                                hoverEnabled: true
                                onEntered: {
                                    logBtn.color = "#333333"
                                }
                                onPressed: {
                                    logBtn.color = "#111111"

                                    //var res = "{\"code\":0}"
                                    //client.setConfig("")
                                    var res = client.login(filedUsr.text,filedPawd.text)
                                    var jres = JSON.parse(res)
                                    if (jres["code"] === 0) {
                                        window.m_token = jres["data"]["token"]
                                        window.m_blog = true;
                                        m_email = filedUsr.text
                                        user_button.text = filedUsr.text
                                        login_dlg.hide()
                                        tip.showmsg("登录成功")
                                    }
                                    else
                                    {
                                        tip_log.showmsg(jres["message"])
                                    }

                                    //window.m_token = jres["token"]
                                    //console.info(window.m_token)
                                }
                                onReleased: {
                                    logBtn.color = "#333333"
                                }

                                onExited: {
                                    logBtn.color = "#6facf9"
                                }
                            }
                        }
                    }


                }

            }





    header: ToolBar {
        id: header_bar
        contentHeight: toolButton.implicitHeight
        RowLayout {
        anchors.fill: parent
        ToolButton {
            id: toolButton
            text: "\u2630"
            //text: stackView.depth > 1 ? "\u25C0" : "\u2630"
            font.pixelSize: Qt.application.font.pixelSize * 1.6
            onClicked: {
//                if (stackView.depth > 1) {
//                    stackView.pop()
//                } else {
                if ( m_blog === false )
                {
                    tip.showmsg("未登录，请先登录！")
                    return
                }
                    drawer.open()
//                }
            }
        }

//        Text {
//            anchors.left: toolButton.right
//            text: "Sugar"
//            verticalAlignment: Qt.AlignVCenter
//            font.pixelSize: 24
//            font.bold: true
//            color: "white"
//        }

        Label {
            text: stackView.currentItem.title
            anchors.centerIn: parent
        }




        ToolButton {
            Menu{
                id : popupMenu
                title: "&File"
    //            anchors.centerIn: parent
                y: user_button.height
                //title: qsTr("Jquery学堂")
//                Action { text: qsTr("Jquery插件")}
//                Action { text: qsTr("Jquery学堂") }
//                Action { text: qsTr("学习资料库") }
//                Action { text: qsTr("QQ群堂") }
//                Action { text: qsTr("TAGS标签") }
//                Action { text: qsTr("在线留言") }
                MenuItem {
                    text: "登录"
                    //shortcut: "Ctrl+X"
                    onTriggered: {
                        login_dlg.reset()
                        login_dlg.show()
                    }
                    enabled: !window.m_blog
                }

                MenuItem {
                    text: "注册"
                    onTriggered: {
                        register_page.reset()
                        register_page.show()
                    }

                    enabled: !window.m_blog
                }

                MenuSeparator{

                }

                MenuItem {
                    text: "修改密码"
                    enabled: window.m_blog
                    onTriggered: {
                        password_reset_page.m_email = m_email
                        password_reset_page.reset()
                        password_reset_page.show()
                    }
                }

                MenuItem {
                    text: "注销"
                    enabled: window.m_blog
                    //shortcut: "Ctrl+X"
                    onTriggered: {
//                        messageDialog.icon = StandardIcon.Question
//                        messageDialog.standardButtons =  StandardButton.No | StandardButton.Yes
//                        messageDialog.setText("是否注销？")
                        messageDialog.open()
                        //if ( messageDialog.Yes)
//                        user_button.text = "未登录"
//                        window.m_blog = false
//                        window.m_token = ""
                    }

                    MessageDialog {
                        id: messageDialog
                        icon: StandardIcon.Question
                        modality: Qt.ApplicationModal
                        standardButtons: StandardButton.No | StandardButton.Yes
                        title: "注销"
                        text: "是否注销？"
                        onAccepted: {
                            console.log("And of course you could only agree.")
                        }
                        onYes: {
                            user_button.text = "未登录"
                            window.m_blog = false
                            window.m_token = ""
                            while (stackView.depth > 1) {
                                stackView.pop()
//                                stackView.update()
                            }
                            tip.showmsg("注销成功")
                        }

                //        Component.onCompleted: visible = true
                    }
                }



            }
//            Image {
//                                id: image_user
//                                height: 16
//                                width: 16
//                                anchors.verticalCenter: parent.verticalCenter
//                                source: "qrc:/images/user.jpeg"
//                            }

            id: user_button
            anchors.right: parent.right
            icon.source: "/images/images/user.jpeg"
            icon.height: user_button.height*0.8
            icon.width: user_button.height*0.8
            icon.color: "transparent"
            text: "未登录"
            //text: stackView.depth > 1 ? "\u25C0" : "\u2630"
            font.pixelSize: Qt.application.font.pixelSize
            onClicked: {
//                if (stackView.depth > 1) {
//                    stackView.pop()
//                } else {
                    //popupMenu.popup(user_button)
                    popupMenu.open()
//                }
            }

        }
        }
    }

    function updateModelC()
    {
        if ( model_page.m_mode === 0 )
        {
            updateModel()
        }
        else if ( model_page.m_mode === 1 )
        {
            updateGrantModel()
        }
        else
        {
            updatePublicModel()
        }
    }

    function updatePublicModel()
    {
        model_page.title = "公共模型"
        model_page.m_mode = 2
        model_page.removeAll()
        client.setConfig(m_token)
        var res = client.list_model_public()
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return -1
        }
        tip.showmsg("获取模型成功")
        var data = jres["data"]
        for ( var i=0; i<data.length ; i++ )
        {
            data[i]["uid"] = data[i]["id"]
            if ( data[i]["role"] === null )
            {
                data[i]["role"] = 2
            }

            if ( data[i]["role"] === 0 )
            {
                data[i]["role_text"] = "创建者"
            }
            else if (data[i]["role"] === 1)
            {
                data[i]["role_text"] = "管理者"
            }
            else if (data[i]["role"] === 2)
            {
                data[i]["role_text"] = "编辑者"
            }
            else if (data[i]["role"] === 3)
            {
                data[i]["role_text"] = "使用者"
            }
            //data[i]["create_date"] = data[i]["create_date"]//.substring(0,19).replace(/T/," ")
            model_page.addItem(jres["data"][i])
        }
        return 0
    }

    function updateGrantModel()
    {
        model_page.title = "被授权的模型"
        model_page.m_mode = 1
        model_page.removeAll()
        client.setConfig(m_token)
        var res = client.list_model_grant()
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return -1
        }
        tip.showmsg("获取模型成功")
        var data = jres["data"]
        for ( var i=0; i<data.length ; i++ )
        {
            data[i]["uid"] = data[i]["id"]
            if ( data[i]["role"] === 0 )
            {
                data[i]["role_text"] = "创建者"
            }
            else if (data[i]["role"] === 1)
            {
                data[i]["role_text"] = "管理者"
            }
            else if (data[i]["role"] === 2)
            {
                data[i]["role_text"] = "编辑者"
            }
            else if (data[i]["role"] === 3)
            {
                data[i]["role_text"] = "使用者"
            }
            //data[i]["create_date"] = data[i]["create_date"]//.substring(0,19).replace(/T/," ")
            model_page.addItem(jres["data"][i])
        }
        return 0
    }

    function updateModel() {
        model_page.title = "我创建的模型"
        model_page.m_mode = 0
        model_page.removeAll()
        client.setConfig(m_token)
        var res = client.list_model()
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return -1
        }
        tip.showmsg("获取模型成功")
        var data = jres["data"]
        for ( var i=0; i<data.length ; i++ )
        {
            data[i]["uid"] = data[i]["id"]
            data[i]["role"] = 0
            data[i]["role_text"] = "创建者"
            data[i]["creator"] = user_button.text
            //data[i]["create_date"] = data[i]["create_date"]//.substring(0,19).replace(/T/," ")
            model_page.addItem(jres["data"][i])
        }
        return 0
    }

    function view_model_version(uid, version)
    {
        client.setConfig(m_token)
        var res = client.get_model_by_version(uid, version)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            model_create_page.title = "查看历史模型"
            model_create_page.m_mode = 2
            model_create_page.m_content = Qt.atob(jres["data"]["model_file_bytes"])
            model_create_page.m_dependencies = jres["data"]["dependencies"]
            model_create_page.m_description = jres["data"]["description"]
            model_create_page.m_uid = uid
            model_create_page.m_model_name = jres["data"]["name"]
            model_create_page.m_model_class = jres["data"]["model_class"]
            model_create_page.m_btn = "修改"
            model_create_page.m_pb = jres["data"]["public"]
            model_create_page.m_version = version
//            model_create_page.m_commit = jres["data"]["commit_message"]
            model_create_page.updateMode()
            //model_create_page.m_from = from
            //model_create_page.setToken(m_token)
            //model_create_page.show()
//            if (stackView.depth > 1) {
//                stackView.pop()
//            }
            stackView.push(model_create_page)
            return
        }
        tip.showmsg(jres["message"])
    }

    function view_model(uid)
    {
        client.setConfig(m_token)
        var res = client.get_model(uid)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            model_create_page.title = "查看模型"
            model_create_page.m_mode = 2
            model_create_page.m_content = Qt.atob(jres["data"]["model_file_bytes"])
            model_create_page.m_dependencies = jres["data"]["dependencies"]
            model_create_page.m_description = jres["data"]["description"]
            model_create_page.m_uid = uid
            model_create_page.m_model_name = jres["data"]["name"]
            model_create_page.m_model_class = jres["data"]["model_class"]
            model_create_page.m_btn = "修改"
            model_create_page.m_pb = jres["data"]["public"]
            model_create_page.m_version = jres["data"]["commit_id"]
//            model_create_page.m_commit = jres["data"]["commit_message"]
            model_create_page.updateMode()
            //model_create_page.m_from = from
            //model_create_page.setToken(m_token)
            //model_create_page.show()
//            if (stackView.depth > 1) {
//                stackView.pop()
//            }
            stackView.push(model_create_page)
            return
        }
        tip.showmsg(jres["message"])
    }

    function edit_model(uid)
    {
        client.setConfig(m_token)
        var res = client.get_model(uid)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            model_create_page.title = "编辑模型"
            model_create_page.m_mode = 0
            model_create_page.m_content = Qt.atob(jres["data"]["model_file_bytes"])
            model_create_page.m_dependencies = jres["data"]["dependencies"]
            model_create_page.m_description = jres["data"]["description"]
            model_create_page.m_uid = uid
            model_create_page.m_model_name = jres["data"]["name"]
            model_create_page.m_model_class = jres["data"]["model_class"]
            model_create_page.m_version = jres["data"]["commit_id"]
            model_create_page.m_btn = "修改"
            model_create_page.updateMode()
            //model_create_page.m_from = from
            //model_create_page.setToken(m_token)
            //model_create_page.show()
//            if (stackView.depth > 1) {
//                stackView.pop()
//            }
            stackView.push(model_create_page)
            return
        }
        tip.showmsg(jres["message"])

    }

    function delete_model(uid)
    {
        client.setConfig(m_token)
        var res = client.delete_model(uid)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 )
        {
            tip.showmsg("删除模型成功")
        }
        else
        {
            tip.showmsg(jres["message"])
        }
        updateModelC()
//        if ( from === 0 )
//        {
//            updateModel()
//        }
//        else
//        {
//            updateGrantModel()
//        }
    }

    function create_model(model_name, model_class, dependencies, content, description, type, filename, pb, commit)
    {
        client.setConfig(m_token)
        var res = client.create_model(model_name, model_class, dependencies, content, description, type, filename, pb, commit)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 )
        {
            tip.showmsg("创建模型成功")
            if (stackView.depth > 1) {
                stackView.pop()
            }
            //stackView.push(model_page)
            updateModelC()
//            if ( from === 0 )
//            {
//                updateModel()
//            }
//            else
//            {
//                updateGrantModel()
//            }
            return
        }
        else
        {
             tip.showmsg(jres["message"])
        }
    }

    function edit_model2(uid, model_name, model_class, dependencies, content, description, type, filename, pb, commit)
    {
        client.setConfig(m_token)
        var res = client.edit_model(uid, model_name, model_class, dependencies, content, description, type, filename, pb, commit)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            tip.showmsg("编辑模型成功")
            if (stackView.depth > 1) {
                stackView.pop()
            }
            updateModelC()
            //stackView.push(model_page)
//            if ( from === 0 )
//            {
//                updateModel()
//            }
//            else
//            {
//                updateGrantModel()
//            }
        }
        else
        {
            tip.showmsg(jres["message"])
        }
    }

    function create_model1()
    {
        model_create_page.m_mode = 1
        model_create_page.title = "创建模型"
        model_create_page.m_content = ""
        model_create_page.m_dependencies = ""
        model_create_page.m_description = ""
        model_create_page.m_uid = ""
        model_create_page.m_model_name = ""
        model_create_page.m_model_class = ""
        model_create_page.m_btn = "创建"
        model_create_page.m_commit = ""
        model_create_page.updateMode()
        //model_create_page.m_from = from
        //model_create_page.setToken(m_token)
        //model_create.show()
//        if (stackView.depth > 1) {
//            stackView.pop()
//        }
        stackView.push(model_create_page)
    }

    function cancel_model()
    {
        if (stackView.depth > 1) {
            stackView.pop()
        }
        stackView.currentItem.updateContent()
        //stackView.push(model_page)
        //tip.showmsg("创建模型成功")
        //model_page.setToken(m_token)
//        if ( from === 0 )
//        {
//            if ( updateModel() === 0 )
//            {
//                tip.showmsg("获取模型成功")
//            }
//        }
//        else
//        {
//            if ( updateGrantModel() === 0 )
//            {
//                tip.showmsg("获取模型成功")
//            }
//        }
    }

    ModelForm {
        id: model_page
        visible: false

        Component.onCompleted: {
            editModel.connect(edit_model)
            delModel.connect(delete_model)
            createModel.connect(create_model1)
            grantModel.connect(grant_model)
            viewModel.connect(view_model)
            updateContent.connect(updateModelC)
            manageModelUser.connect(manage_model_user)
        }


    }

    ModelCreate {
        id: model_create_page
        visible: false
        width: parent.width
        height: parent.height
        Component.onCompleted: {
            createModel.connect(create_model)
            editModel.connect(edit_model2)
            cancel.connect(cancel_model)
        }
    }

    function grant_model(uid, model_name)
    {
        model_grant_page.title = "授权数据集"
        //model_grant_page.m_from = mode
        model_grant_page.m_uid = uid
        model_grant_page.m_model_name = model_name
        model_grant_page.clear()
//        if (stackView.depth > 1) {
//            stackView.pop()
//        }
        stackView.push(model_grant_page)
    }

    function grant_model2(model_id, email, role)
    {
        client.setConfig(m_token)
        var res = client.grant_model(model_id, email, role)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            tip.showmsg("授权模型成功！")
            if (stackView.depth > 1) {
                stackView.pop()
            }
            updateModelC()
            //stackView.push(model_page)
//            if ( from === 0 )
//            {
//                updateModel()
//            }
//            else
//            {
//                updateGrantModel()
//            }
            return
        }
        tip.showmsg(jres["message"])
    }

    function manage_model_user(uid, model_name, role)
    {
//        if (stackView.depth > 1) {
//            stackView.pop()
//        }
        model_user_page.m_id = uid
        model_user_page.title = "模型"+model_name+"成员列表"
        model_user_page.m_role = role
        stackView.push(model_user_page)
        updateModelUser(uid)
    }

    ModelGrantForm {
        id: model_grant_page
        visible: false
        width: parent.width
        height: parent.height
        Component.onCompleted: {
            error.connect(show_error)
            grantModel.connect(grant_model2)
            cancel.connect(cancel_model)
        }
    }

    function updateModelUser(model_id)
    {
        //dataset_user_page.title = "模型成员列表"
        model_user_page.removeAll()
        client.setConfig(m_token)
        var res = client.list_model_user(model_id)
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return -1
        }
        tip.showmsg("获取模型成员列表成功")
        var data = jres["data"]
        for ( var i=0; i<data.length ; i++ )
        {
            data[i]["uid"] = data[i]["id"]
//            data[i]["role"] = 0
//            data[i]["role_text"] = "创建者"
//            data[i]["creator"] = user_button.text
            //data[i]["create_date"] = data[i]["create_date"]//.substring(0,19).replace(/T/," ")
            model_user_page.addItem(jres["data"][i])
        }
        res = client.get_model(model_id)
        jres = JSON.parse(res)
        if ( jres["code"] === 0 )
        {
            model_user_page.m_pb = jres["data"]["public"]
            model_user_page.update()
            return 0
        }
        tip.showmsg(jres["message"])
        return 0
    }

    function change_model_user_role(email, model_id, role)
    {
        client.setConfig(m_token)
        var res = client.grant_model(model_id, email, role)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            tip.showmsg("授权模型成功")

//            if (stackView.depth > 1) {
//                stackView.pop()
//            }
            //stackView.push(dataset_page)
            //updateModelUser(model_id)
//            if ( dataset_page.m_mode === 0 )
//            {
//                updateDataset()
//            }
//            else
//            {
//                updateGrantDataset()
//            }
            return
        }
        tip.showmsg(jres["message"])
    }

    function delete_model_user(model_id, grant_id)
    {
        client.setConfig(m_token)
        var res = client.delete_model_user(model_id, grant_id)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            tip.showmsg("删除用户成功")
            updateModelUser(model_id)
        }
        tip.showmsg(jres["message"])
    }

    function updateModelPublic(model_id, ipb)
    {
        client.setConfig(m_token)
        var res = client.change_model_public(model_id, ipb)
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return -1
        }
        tip.showmsg("修改模型公有权限成功")
    }

    ModelGrantUser {
        id: model_user_page
        visible: false
        width: parent.width
        height: parent.height
        Component.onCompleted: {
            error.connect(show_error)
            changeUserRole.connect(change_model_user_role)
            backModel.connect(cancel_model)
            delGrant.connect(delete_model_user)
            changeModelPublic.connect(updateModelPublic)
        }
    }



    function updateDatasetC()
    {
        if ( dataset_page.m_mode === 0 )
        {
            updateDataset()
        }
        else if ( dataset_page.m_mode === 1 )
        {
            updateGrantDataset()
        }
        else
        {
            updatePublicDataset()
        }
    }

    function updatePublicDataset()
    {
        dataset_page.title = "公共数据集"
        dataset_page.m_mode = 2
        dataset_page.removeAll()
        client.setConfig(m_token)
        var res = client.list_dataset_public()
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return -1
        }
        tip.showmsg("获取数据集成功")
        var data = jres["data"]
        for ( var i=0; i<data.length ; i++ )
        {
            data[i]["uid"] = data[i]["id"]
            if ( data[i]["role"] === null )
            {
                data[i]["role"] = 3
            }

            if ( data[i]["role"] === 0 )
            {
                data[i]["role_text"] = "创建者"
            }
            else if (data[i]["role"] === 1)
            {
                data[i]["role_text"] = "管理者"
            }
            else if (data[i]["role"] === 2)
            {
                data[i]["role_text"] = "编辑者"
            }
            else if (data[i]["role"] === 3)
            {
                data[i]["role_text"] = "使用者"
            }
            //data[i]["create_date"] = data[i]["create_date"]//.substring(0,19).replace(/T/," ")
            dataset_page.addItem(jres["data"][i])
        }
        return 0
    }

    function updateGrantDataset()
    {
        dataset_page.title = "被授权的数据集"
        dataset_page.m_mode = 1
        dataset_page.removeAll()
        client.setConfig(m_token)
        var res = client.list_dataset_grant()
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return -1
        }
        tip.showmsg("获取数据集成功")
        var data = jres["data"]
        for ( var i=0; i<data.length ; i++ )
        {
            data[i]["uid"] = data[i]["id"]
            if ( data[i]["role"] === 0 )
            {
                data[i]["role_text"] = "创建者"
            }
            else if (data[i]["role"] === 1)
            {
                data[i]["role_text"] = "管理者"
            }
            else if (data[i]["role"] === 2)
            {
                data[i]["role_text"] = "编辑者"
            }
            else if (data[i]["role"] === 3)
            {
                data[i]["role_text"] = "使用者"
            }
            //data[i]["create_date"] = data[i]["create_date"]//.substring(0,19).replace(/T/," ")
            dataset_page.addItem(jres["data"][i])
        }
        return 0
    }

    function updateDataset()
    {
        dataset_page.title = "我创建的数据集"
        dataset_page.m_mode = 0
        dataset_page.removeAll()
        client.setConfig(m_token)
        var res = client.list_dataset()
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return -1
        }
        tip.showmsg("获取数据集成功")
        var data = jres["data"]
        for ( var i=0; i<data.length ; i++ )
        {
            data[i]["uid"] = data[i]["id"]
            data[i]["role"] = 0
            data[i]["role_text"] = "创建者"
            data[i]["creator"] = user_button.text
            //data[i]["create_date"] = data[i]["create_date"]//.substring(0,19).replace(/T/," ")
            dataset_page.addItem(jres["data"][i])
        }
        return 0
    }

    function create_dataset1()
    {
        dataset_create_page.title = "创建数据集"
        dataset_create_page.m_uid = ""
        dataset_create_page.m_mode = 1
        dataset_create_page.m_dataset_path = ""
        dataset_create_page.m_dataset_name = ""
        dataset_create_page.m_dataset_description = ""
        dataset_create_page.m_btn = "创建"
//        dataset_create_page.m_from = mode
        dataset_create_page.updateMode()
//        if (stackView.depth > 1) {
//            stackView.pop()
//        }
        stackView.push(dataset_create_page)
    }

    function create_dataset(dataset_name, dataset_path, dataset_description, pb)
    {
        client.setConfig(m_token)
        var res = client.create_dataset(dataset_name, dataset_path, dataset_description, pb)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            tip.showmsg("创建数据集成功")
            if (stackView.depth > 1) {
                stackView.pop()
            }
            //stackView.push(dataset_page)
            updateDatasetC()

            return
        }
        tip.showmsg(jres["message"])
    }

    function edit_dataset(uid)
    {
        client.setConfig(m_token)
        var res = client.get_dataset(uid)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            dataset_create_page.title = "编辑数据集"
            dataset_create_page.m_uid = uid
            dataset_create_page.m_mode = 0
            dataset_create_page.m_dataset_path = ""
            dataset_create_page.m_btn = "修改"
            dataset_create_page.m_dataset_name = jres["data"]["name"]
            dataset_create_page.m_dataset_description = jres["data"]["description"]
            dataset_create_page.m_pb = jres["data"]["public"]
//            dataset_create_page.m_from = mode
            dataset_create_page.updateMode()
//            if (stackView.depth > 1) {
//                stackView.pop()
//            }
            stackView.push(dataset_create_page)
            return
        }
        tip.showmsg(jres["message"])
    }

    function view_dataset(uid)
    {
        client.setConfig(m_token)
        var res = client.get_dataset(uid)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            dataset_create_page.title = "查看数据集"
            dataset_create_page.m_uid = uid
            dataset_create_page.m_mode = 2
            dataset_create_page.m_dataset_path = ""
            //dataset_create_page.m_btn = "修改"
            dataset_create_page.m_dataset_name = jres["data"]["name"]
            dataset_create_page.m_dataset_description = jres["data"]["description"]
            dataset_create_page.m_pb = jres["data"]["public"]
//            dataset_create_page.m_from = mode
            dataset_create_page.updateMode()
//            if (stackView.depth > 1) {
//                stackView.pop()
//            }
            stackView.push(dataset_create_page)
            return
        }
        tip.showmsg(jres["message"])
    }

    function delete_dataset(uid)
    {
        client.setConfig(m_token)
        var res = client.delete_dataset(uid)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 )
        {
            tip.showmsg("删除数据集成功")
        }
        else
        {
            tip.showmsg(jres["message"])
        }
        updateDatasetC()
    }

    function edit_dataset2(uid, dataset_name, dataset_description)
    {
        client.setConfig(m_token)
        var res = client.edit_dataset(uid, dataset_name, dataset_description)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            tip.showmsg("编辑数据集成功")
            if (stackView.depth > 1) {
                stackView.pop()
            }
            //stackView.push(dataset_page)
            updateDatasetC()
            return
        }
        tip.showmsg(jres["message"])
    }

    function cancel_dataset()
    {
        if (stackView.depth > 1) {
            stackView.pop()
        }
        stackView.currentItem.updateContent()
//        stackView.push(dataset_page)
//        if ( from === 0 )
//        {
//            if ( updateDataset() === 0 )
//            {
//                tip.showmsg("获取数据集成功")
//            }
//        }
//        else
//        {
//            if ( updateGrantDataset() === 0 )
//            {
//                tip.showmsg("获取数据集成功")
//            }
//        }
    }

    DatasetForm {
        id: dataset_page
        visible: false
//        onCreateDataset: {
//            dataset_create_page.title = "创建数据集"
//            dataset_create_page.m_uid = ""
//            dataset_create_page.m_mode = 1
//            dataset_create_page.m_dataset_path = ""
//            dataset_create_page.m_dataset_name = ""
//            dataset_create_page.m_dataset_description = ""
//            dataset_create_page.m_btn = "创建"
//            dataset_create_page.updateMode()
//            if (stackView.depth > 1) {
//                stackView.pop()
//            }
//            stackView.push(dataset_create_page)
//        }
        Component.onCompleted: {
            editDataset.connect(edit_dataset)
            delDataset.connect(delete_dataset)
            createDataset.connect(create_dataset1)
            grantDataset.connect(grant_dataset)
            updateContent.connect(updateDatasetC)
            viewDataset.connect(view_dataset)
            manageDatasetUser.connect(manager_dataset_user)
        }
    }

    DatasetCreate {
        id: dataset_create_page
        visible: false
        width: parent.width
        height: parent.height
        Component.onCompleted: {
            createDataset.connect(create_dataset)
            editDataset.connect(edit_dataset2)
            cancel.connect(cancel_dataset)
        }
//        onCancel: {
//            if (stackView.depth > 1) {
//                stackView.pop()
//            }
//            stackView.push(dataset_page)
//            if ( updateDataset() === 0 )
//            {
//                tip.showmsg("获取数据集成功")
//            }
//        }
    }

    function grant_dataset(uid, dataset_name)
    {
        dataset_grant_page.title = "授权数据集"
//        dataset_grant_page.m_from = mode
        dataset_grant_page.m_uid = uid
        dataset_grant_page.m_dataset_name = dataset_name
        dataset_grant_page.clear()
//        if (stackView.depth > 1) {
//            stackView.pop()
//        }
        stackView.push(dataset_grant_page)
    }

    function grant_dataset2(dataset_id, email, role)
    {
        client.setConfig(m_token)
        var res = client.grant_dataset(dataset_id, email, role)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            tip.showmsg("授权数据集成功")
            if (stackView.depth > 1) {
                stackView.pop()
            }
            //stackView.push(dataset_page)
            updateDatasetC()
//            if ( dataset_page.m_mode === 0 )
//            {
//                updateDataset()
//            }
//            else
//            {
//                updateGrantDataset()
//            }
            return
        }
        tip.showmsg(jres["message"])
    }

    function manager_dataset_user(uid, dataset_name, role)
    {
//        if (stackView.depth > 1) {
//            stackView.pop()
//        }
        dataset_user_page.ds_id = uid
        dataset_user_page.title = "数据集"+dataset_name+"成员列表"
        dataset_user_page.m_role = role
        stackView.push(dataset_user_page)
        updateDatasetUser(uid)
    }

    DatasetGrantForm {
        id: dataset_grant_page
        visible: false
        width: parent.width
        height: parent.height
        Component.onCompleted: {
            error.connect(show_error)
            grantDataset.connect(grant_dataset2)
            cancel.connect(cancel_dataset)
        }
    }

    function delete_dataset_user(dataset_id, grant_id)
    {
        client.setConfig(m_token)
        var res = client.delete_dataset_user(dataset_id, grant_id)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            tip.showmsg("删除用户成功")
            updateDatasetUser(dataset_id)
        }
        tip.showmsg(jres["message"])
    }

    function updateDatasetUser(dataset_id)
    {
        //dataset_user_page.title = "数据集成员列表"


        dataset_user_page.removeAll()
        client.setConfig(m_token)
        var res = client.list_dataset_user(dataset_id)
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return -1
        }
        tip.showmsg("获取数据集成员列表成功")
        var data = jres["data"]
        for ( var i=0; i<data.length ; i++ )
        {
            data[i]["uid"] = data[i]["id"]
//            data[i]["role"] = 0
//            data[i]["role_text"] = "创建者"
//            data[i]["creator"] = user_button.text
            //data[i]["create_date"] = data[i]["create_date"]//.substring(0,19).replace(/T/," ")
            dataset_user_page.addItem(jres["data"][i])
        }
        client.setConfig(m_token)
        res = client.get_dataset(dataset_id)
        jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            dataset_user_page.m_pb = jres["data"]["public"]
            dataset_user_page.update()
            return 0
        }
        tip.showmsg(jres["message"])
        return 0
    }

    function change_dataset_user_role(email, dataset_id, role)
    {
        client.setConfig(m_token)
        var res = client.grant_dataset(dataset_id, email, role)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            tip.showmsg("授权数据集成功")
//            if (stackView.depth > 1) {
//                stackView.pop()
//            }
            //stackView.push(dataset_page)
            //updateDatasetUser(dataset_id)
//            if ( dataset_page.m_mode === 0 )
//            {
//                updateDataset()
//            }
//            else
//            {
//                updateGrantDataset()
//            }
            return
        }
        tip.showmsg(jres["message"])
    }

    function updateDatasetPublic(dataset_id, ipb)
    {
        client.setConfig(m_token)
        var res = client.change_dataset_public(dataset_id, ipb)
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return -1
        }
        tip.showmsg("修改数据集公有权限成功")
    }

    DatasetGrantUser {
        id: dataset_user_page
        visible: false
        width: parent.width
        height: parent.height
        Component.onCompleted: {
            error.connect(show_error)
            changeUserRole.connect(change_dataset_user_role)
            backDataset.connect(cancel_dataset)
            delGrant.connect(delete_dataset_user)
            changeDatasetPublic.connect(updateDatasetPublic)
        }
    }

    function updateTrainjob()
    {
        trainjob_page.removeAll()
        client.setConfig(m_token)
        var res = client.list_trainjob()
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return -1
        }
        tip.showmsg("获取训练任务成功")
        var data = jres["data"]
        for ( var i=0; i<data.length ; i++ )
        {
            data[i]["uid"] = data[i]["id"]
            //data[i]["datatime_started"] = data[i]["datatime_started"]//.substring(0,19).replace(/T/," ")
            trainjob_page.addItem(jres["data"][i])
        }
        trainjob_page.update()
        return 0
    }

    function create_trainjob(train_dataset_id, val_dataset_id, model_id, trainjob_name, trainjob_description, budget, trainjob_param, trainjob_version)
    {
        client.setConfig(m_token)
        var res = client.create_trainjob(train_dataset_id, val_dataset_id, model_id, trainjob_name, trainjob_description, budget, trainjob_param, trainjob_version)
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 ) {
            tip.showmsg("创建训练任务成功")
            if (stackView.depth > 1) {
                stackView.pop()
            }
            //stackView.push(trainjob_page)
            updateTrainjob()
            return
        }
        tip.showmsg(jres["message"])
    }

    function stop_trainjob(uid)
    {
        client.setConfig(m_token)
        var res = client.stop_trainjob(uid, "STOPPED")
        var jres = JSON.parse(res)
        if ( jres["code"] === 0 )
        {
            tip.showmsg("停止训练任务成功")
            updateTrainjob()
        }
        else {
            tip.showmsg(jres["message"])
        }
    }

    function get_trainjob(uid)
    {
        trainjob_create_page.title = "查看训练"
        trainjob_create_page.m_uid = uid
        //trainjob_create_page.setToken(m_token)
        trainjob_create_page.setMode(0)
        trainjob_create_page.updateMode()
        trainjob_create_page.removeAllDatasetItem()
        client.setConfig(m_token)
        var res_trainjob = client.get_trainjob(uid)
        var jres_trainjob = JSON.parse(res_trainjob)
        if ( jres_trainjob["code"] !== 0 )
        {
            tip.showmsg(jres_trainjob["message"])
            return
        }
        var budget = JSON.parse(jres_trainjob["data"]["budget"])

        var res = client.list_dataset_grant()
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return
        }
        //tip.showmsg("获取数据集成功")
        var data = jres["data"]
        var train_dataset_index = -1
        var val_dataset_index = -1
        for ( var i=0; i<data.length ; i++ )
        {
            data[i]["uid"] = data[i]["id"]
            data[i]["text"] = data[i]["name"]
            data[i]["color"] = "black"
            if (data[i]["uid"] === jres_trainjob["data"]["train_dataset_id"])
            {
                train_dataset_index = i
            }
            if (data[i]["uid"] === jres_trainjob["data"]["val_dataset_id"])
            {
                val_dataset_index = i
            }
            trainjob_create_page.addDatasetItem(jres["data"][i])
        }

        trainjob_create_page.removeModelAll()
        client.setConfig(m_token)
        res = client.list_model_grant()
        jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return
        }
        //tip.showmsg("获取模型成功")
        data = jres["data"]
        var model_index = -1
        for ( i=0; i<data.length ; i++ )
        {
            data[i]["uid"] = data[i]["id"]
            data[i]["text"] = data[i]["name"]
            data[i]["color"] = "black"
            if (data[i]["uid"] === jres_trainjob["data"]["model_id"])
            {
                model_index = i
            }
            trainjob_create_page.addModelItem(jres["data"][i])
        }

        trainjob_create_page.setConfig(jres_trainjob["data"]["name"], jres_trainjob["data"]["description"], budget["HOURS"], budget["GPU_COUNT"], jres_trainjob["data"]["train_args"], jres_trainjob["data"]["version"], train_dataset_index, val_dataset_index, model_index, jres_trainjob["data"]["model_commit_id"])
//        if (stackView.depth > 1) {
//            stackView.pop()
//        }
        stackView.push(trainjob_create_page)
    }

    TrainjobForm {
        id: trainjob_page
        visible: false
        Component.onCompleted: {
            stopTrainjob.connect(stop_trainjob)
            getTrainjob.connect(get_trainjob)
            showTrainjobLog.connect(show_trainjob_log)
            updateContent.connect(updateTrainjob)

        }
        onCreateTrainjob: {
            trainjob_create_page.title = "创建训练"
            //trainjob_create_page.setToken(m_token)
            trainjob_create_page.setMode(1)
            trainjob_create_page.removeAllDatasetItem()
            client.setConfig(m_token)
            var res = client.list_dataset_available()
            var jres = JSON.parse(res)
            if ( jres["code"] !== 0 )
            {
                tip.showmsg(jres["message"])
                return
            }
            //tip.showmsg("获取数据集成功")
            var data = jres["data"]
            for ( var i=0; i<data.length ; i++ )
            {
                data[i]["uid"] = data[i]["id"]
                if ( data[i]["public"] === 1 )
                {
                    data[i]["text"] = data[i]["name"]+"        public"
                }
                else
                {
                    data[i]["text"] = data[i]["name"]+"        "+data[i]["creator"]
                }
                data[i]["color"] = "black"
                trainjob_create_page.addDatasetItem(jres["data"][i])
            }

            trainjob_create_page.removeModelAll()
            client.setConfig(m_token)
            res = client.list_model_available()
            jres = JSON.parse(res)
            if ( jres["code"] !== 0 )
            {
                tip.showmsg(jres["message"])
                return
            }
            //tip.showmsg("获取模型成功")
            data = jres["data"]
            for ( i=0; i<data.length ; i++ )
            {
                data[i]["uid"] = data[i]["id"]
                if ( data[i]["public"] === 1 )
                {
                    data[i]["text"] = data[i]["name"]+"        public"
                }
                else
                {
                    data[i]["text"] = data[i]["name"]+"        "+data[i]["creator"]
                }

                data[i]["color"] = "black"
                trainjob_create_page.addModelItem(jres["data"][i])
            }


            trainjob_create_page.updateMode()
//            if (stackView.depth > 1) {
//                stackView.pop()
//            }
            stackView.push(trainjob_create_page)
        }
    }

    TrainjobCreate {
        id: trainjob_create_page
        visible: false
        width: parent.width
        height: parent.height
        Component.onCompleted: {
            createTrainjob.connect(create_trainjob)

            error.connect(show_error)

            viewDataset.connect(view_dataset)
            viewModel.connect(view_model)
            viewModelVersion.connect(view_model_version)
//            getTrain.connect(edit_dataset2)
        }
        onCancel: {
            if (stackView.depth > 1) {
                stackView.pop()
            }
            stackView.currentItem.updateContent()
//            stackView.push(trainjob_page)
//            if ( updateTrainjob() === 0 )
//            {
//                tip.showmsg("获取训练任务成功")
//            }
        }

    }

    TrainjobLog {
        id: trainjob_log_page
        visible: false
        width: parent.width
        height: parent.height
        onCancel: {
            if (stackView.depth > 1) {
                stackView.pop()
            }
            stackView.currentItem.updateContent()
//            stackView.push(trainjob_page)
//            if ( updateTrainjob() === 0 )
//            {
//                tip.showmsg("获取训练任务成功")
//            }
        }
    }

    function show_trainjob_log(uid)
    {
        client.setConfig(m_token)
        var res_trainjob = client.get_trainjob(uid)
        var jres_trainjob = JSON.parse(res_trainjob)
        if ( jres_trainjob["code"] !== 0 )
        {
            tip.showmsg(jres_trainjob["message"])
            return
        }
//        if (stackView.depth > 1) {
//            stackView.pop()
//        }
        stackView.push(trainjob_log_page)
        tip.showmsg("获取训练日志成功")
        var sdescription = "训练ID：" + jres_trainjob["data"]["id"] + "\n" + "开始时间：" + jres_trainjob["data"]["datetime_started"] + "\n" + "结束时间：" + jres_trainjob["data"]["datetime_stopped"] + "\n"
                           + "预算：" + jres_trainjob["data"]["budget"] + "\n" + "状态：" + jres_trainjob["data"]["status"] + "\n"
        trainjob_log_page.setlog(sdescription, jres_trainjob["data"]["log"])
    }


    function updateApplication()
    {
        application_page.removeAll()
        client.setConfig(m_token)
        var res = client.list_application()
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return -1
        }
        tip.showmsg("获取AI应用成功")
        var data = jres["data"]
        for ( var i=0; i<data.length ; i++ )
        {
            data[i]["uid"] = data[i]["id"]
            //data[i]["datatime_started"] = data[i]["datatime_started"]//.substring(0,19).replace(/T/," ")
            application_page.addItem(jres["data"][i])
        }
        return 0
    }

    function stop_application(uid)
    {
        client.setConfig(m_token)
        var res = client.stop_application(uid, "STOPPED")
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return
        }
        tip.showmsg("停止AI应用成功！")
        updateApplication()
    }

    function restart_application(uid)
    {
        client.setConfig(m_token)
        var res = client.restart_application(uid)
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return
        }
        tip.showmsg("启动AI应用成功！")
        updateApplication()
    }

    function get_application(uid)
    {
        client.setConfig(m_token)
        var res = client.get_application(uid)
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return
        }
        var res_trainjob = client.list_trainjob()
        var jres_trainjob = JSON.parse(res_trainjob)
        if ( jres_trainjob["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return
        }
        application_create_page.removeAllTrainjobItem()

        var trainjob_data = jres_trainjob["data"]
        var trainjob_index = -1
        for ( var i=0; i<trainjob_data.length ; i++ )
        {
            trainjob_data[i]["uid"] = trainjob_data[i]["id"]
            trainjob_data[i]["text"] = trainjob_data[i]["name"]
            trainjob_data[i]["color"] = "black"
            if ( trainjob_data[i]["id"] === jres["data"]["train_job_id"] )
            {
                trainjob_index = i
            }
            application_create_page.addTrainjobItem(trainjob_data[i])
        }
        application_create_page.setMode(0)
        application_create_page.updateMode()

        application_create_page.setConfig(jres["data"]["name"], jres["data"]["description"], trainjob_index)
//        if (stackView.depth > 1) {
//            stackView.pop()
//        }
        stackView.push(application_create_page)
    }

    function create_application(name, trainjob_id, description)
    {
        client.setConfig(m_token)
        var res = client.create_application(name, trainjob_id, description)
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return
        }
        if (stackView.depth > 1) {
            stackView.pop()
        }
        //stackView.push(application_page)
        tip.showmsg("创建AI应用成功！")
        updateApplication()
    }



    ApplicationForm {
        id: application_page
        visible: false
        Component.onCompleted: {
            stopApplication.connect(stop_application)
            restartApplication.connect(restart_application)
            getApplication.connect(get_application)
            updateContent.connect(updateApplication)
        }
        onCreateApplication: {
            application_create_page.title = "创建AI应用"
            //trainjob_create_page.setToken(m_token)
            application_create_page.setMode(1)
            application_create_page.removeAllTrainjobItem()
            client.setConfig(m_token)
            var res = client.list_trainjob()
            var jres = JSON.parse(res)
            if ( jres["code"] !== 0 )
            {
                tip.showmsg(jres["message"])
                return
            }
            //tip.showmsg("获取数据集成功")
            var data = jres["data"]
            for ( var i=0; i<data.length ; i++ )
            {
                data[i]["uid"] = data[i]["id"]
                data[i]["text"] = data[i]["name"] + " V" + data[i]["version"]
                data[i]["color"] = "black"
                if (data[i]["status"] === "SUCCESS")
                {
                    application_create_page.addTrainjobItem(jres["data"][i])
                }
            }

            application_create_page.updateMode()
//            if (stackView.depth > 1) {
//                stackView.pop()
//            }
            //stackView.currentItem.updateContent()
            stackView.push(application_create_page)
        }
    }

    ApplicationCreate {
        id: application_create_page
        visible: false
        width: parent.width
        height: parent.height
        Component.onCompleted: {
            createApplication.connect(create_application)
            viewTrainjob.connect(get_trainjob)
            error.connect(show_error)
        }
        onCancel: {
            if (stackView.depth > 1) {
                stackView.pop()
            }
            stackView.currentItem.updateContent()
//            stackView.push(application_page)
//            if ( updateApplication() === 0 )
//            {
//                tip.showmsg("获取AI应用成功")
//            }
        }
    }

    function updateDashboard()
    {
        client.setConfig(m_token)
        var res = client.list_dataset()
        var jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return
        }
        var my_dataset_count = jres["data"].length
        res = client.list_dataset_grant()
        jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return
        }
        var my_dataset_grant_count = jres["data"].length
        dashboard_page.setDataset(my_dataset_count, my_dataset_grant_count)
        res = client.list_model()
        jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return
        }
        var my_model_count = jres["data"].length
        res = client.list_model_grant()
        jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return
        }
        var my_model_grant_count = jres["data"].length
        dashboard_page.setModel(my_model_count, my_model_grant_count)
        res = client.list_trainjob()
        jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return
        }
        var my_trainjob_count = jres["data"].length
        dashboard_page.setTrainjob(my_trainjob_count)

        res = client.list_application()
        jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return
        }
        var my_app_count = jres["data"].length
        dashboard_page.setApplication(my_app_count)

        res = client.get_computer_source()
        jres = JSON.parse(res)
        if ( jres["code"] !== 0 )
        {
            tip.showmsg(jres["message"])
            return
        }
        dashboard_page.setComputerSource(jres["data"]["cpu_allocable"], jres["data"]["cpu_total"], jres["data"]["cpu_per_max"], (jres["data"]["men_allocable"]/1024/1024).toFixed(2), (jres["data"]["men_total"]/1024/1024).toFixed(2), (jres["data"]["men_per_max"]/1024/1024).toFixed(2),jres["data"]["gpu_allocable"], jres["data"]["gpu_total"], jres["data"]["gpu_per_max"])
        dashboard_page.update()
    }

    DashBoard {
        id: dashboard_page
        visible: false
        width: parent.width
        height: parent.height
        Component.onCompleted: {
            updateContent.connect(updateDashboard)
        }
        onMyDataset: {
            stepToMyDataset()
        }
        onMyGrantDataset: {
            stepToMyGrantDataset()
        }
        onMyModel: {
            stepToMyModel()
        }
        onMyGrantModel: {
            stepToMyGrantModel()
        }
        onMyTrainjob: {
            stepToMyTrainjob()
        }
        onMyApplication: {
            stepToMyApplication()
        }
    }

    function stepToMyDataset()
    {
        if (stackView.currentItem !== dataset_page )
        {
            while (stackView.depth > 1) {
                stackView.pop()
            }
            stackView.push(dataset_page)

        }
        dataset_page.m_mode = 0
        dataset_page.updateContent()
    }

    function stepToMyGrantDataset()
    {
        if (stackView.currentItem !== dataset_page )
        {
            while (stackView.depth > 1) {
                stackView.pop()
            }
            stackView.push(dataset_page)

        }
        dataset_page.m_mode = 1
        dataset_page.updateContent()
    }

    function stepToMyModel()
    {
        if (stackView.currentItem !== model_page )
        {
            while (stackView.depth > 1) {
                stackView.pop()
            }
            stackView.push(model_page)

        }
        model_page.m_mode = 0
        model_page.updateContent()
    }

    function stepToMyGrantModel()
    {
        if (stackView.currentItem !== model_page )
        {
            while (stackView.depth > 1) {
                stackView.pop()
            }
            stackView.push(model_page)

        }
        model_page.m_mode = 1
        model_page.updateContent()
    }

    function stepToMyTrainjob()
    {
        if (stackView.currentItem !== trainjob_page )
        {
            while (stackView.depth > 1) {
                stackView.pop()
            }
            stackView.push(trainjob_page)

        }
        trainjob_page.updateContent()
    }

    function stepToMyApplication()
    {
        if (stackView.currentItem !== application_page )
        {
            while (stackView.depth > 1) {
                stackView.pop()
            }
            stackView.push(application_page)

        }
        application_page.updateContent()
    }

    Drawer {
        id: drawer
        width: 300
        height: window.height
        background: Rectangle {
            anchors.fill: drawer
            color: "aliceblue"
        }

//        TreeView {
//            anchors.fill: parent
//            id: tree
//            model: modelTree2
//            width: 200
//        }
//           ListModel {
//              id: modelTree2
//             Component.onCompleted: {
//                 modelTree2.append([
//                     {title: "Node 1"},
//                     {title: "Node 2", items: [
//                          {title: "Node 21", items:[
//                           {title: "Node 211"},
//                         {title: "Node 212"}
//                           ]},
//                        {title: "Node 22"}
//                        ]
//                     },
//                 {title: "Node 3"}
//              ]);
//              }
//        }


        Column {
            anchors.fill: parent
            ItemDelegate {
                width: parent.width
                height: 60
                Rectangle {
                    width: parent.width
                    height: 60
                    color: "steelblue"
                    Text {
                        anchors.centerIn: parent
                        text: qsTr("Sugar")
                        font.bold: true
                        color: "white"
                        //width: parent.width
                        font.pixelSize: 30
                        font.family: "Mistral"//"Bradley Hand"
                    }
                }


            }

            ItemDelegate {
                width: parent.width
                height: 80
                Button {
                    id: drawerDashboard
                    anchors.fill: parent
                    text: qsTr("  总览  ")
                    contentItem: Text {
                        text: drawerDashboard.text
                        font: drawerDashboard.font
                        opacity: enabled ? 1.0 : 0.3
                        color: "black"
                        horizontalAlignment: Text.AlignLeft
                        verticalAlignment: Text.AlignVCenter
                        elide: Text.ElideRight
                    }
                    background: Rectangle {
                        color: drawerDashboard.hovered ?"gray":"aliceblue"
                        anchors.fill: drawerDashboard
//                        Image {
//                            source: "/images/images/dashboard.jpg"
//                            anchors.left: parent.left
//                            anchors.verticalCenter: parent.verticalCenter
//                            width: parent.height*0.3
//                            height: parent.height*0.3
//                            opacity: parent.hovered?0.3:1
//                        }
                    }
                    onClicked: {
                        if (stackView.currentItem !== dashboard_page )
                        {
                            while (stackView.depth > 1) {
                                stackView.pop()
                            }

                            stackView.push(dashboard_page)
                        }

                        dashboard_page.updateContent()
                        drawer.close()
                    }
                }
            }
//            ItemDelegate {
//                text: qsTr("项目组")
//                width: parent.width
//                onClicked: {
//                    if (stackView.depth > 1) {
//                        stackView.pop()
//                    }

//                    stackView.push("ProjectForm.qml")
//                    drawer.close()
//                }
//            }
            ItemDelegate {
                width: parent.width
                height: 80
                Button {
                    id: drawerDataset
                    anchors.fill: parent
                    text:"  数据集"
                    contentItem: Text {
                        text: drawerDataset.text
                        font: drawerDataset.font
                        opacity: enabled ? 1.0 : 0.3
                        color: "black"
                        horizontalAlignment: Text.AlignLeft
                        verticalAlignment: Text.AlignVCenter
                        elide: Text.ElideRight
                    }
                    background: Rectangle {
                        color: drawerDataset.hovered ?"gray":"aliceblue"
                        anchors.fill:drawerDataset
                        Text {
                            text: "▶   "
                            font: drawerDataset.font
                            anchors.right: parent.right
                            height: drawerDataset.height
                            verticalAlignment: Text.AlignVCenter
                        }
                    }


                    onClicked: menuDataset.open()
                    Menu{
                        id: menuDataset
                        title:"dataset"
                        x: drawerDataset.width
                        y: drawerDataset.top
                        MenuItem{
                            text: qsTr("我创建的数据集")
                            width: parent.width
                            height: 80
                            onClicked: {
                                if (stackView.currentItem !== dataset_page )
                                {
                                    while (stackView.depth > 1) {
                                        stackView.pop()
                                    }
                                    stackView.push(dataset_page)

                                }
                                dataset_page.m_mode = 0
                                dataset_page.updateContent()
//                                if ( updateDataset() === 0 )
//                                {
//                                    tip.showmsg("获取数据集成功")
//                                }
                                drawer.close()
                            }
                        }
                        MenuItem{
                            text: qsTr("被授权的数据集")
                            width: parent.width
                            height: 80
                            onClicked: {
                                if (stackView.currentItem !== dataset_page )
                                {
                                    while (stackView.depth > 1) {
                                        stackView.pop()
                                    }
                                    stackView.push(dataset_page)

                                }
                                dataset_page.m_mode = 1
                                dataset_page.updateContent()
//                                if ( updateGrantDataset() === 0 )
//                                {
//                                    tip.showmsg("获取数据集成功")
//                                }
                                drawer.close()
                            }
                        }
                        MenuItem{
                            text: qsTr("公共数据集")
                            width: parent.width
                            height: 80
                            onClicked: {
                                if (stackView.currentItem !== dataset_page )
                                {
                                    while (stackView.depth > 1) {
                                        stackView.pop()
                                    }
                                    stackView.push(dataset_page)

                                }
                                dataset_page.m_mode = 2
                                dataset_page.updateContent()
//                                if ( updateGrantDataset() === 0 )
//                                {
//                                    tip.showmsg("获取数据集成功")
//                                }
                                drawer.close()
                            }
                        }
                    }
                }
            }
            ItemDelegate {
                width: parent.width
                height: 80
                Button {
                    id: drawerModel
                    anchors.fill: parent
                    text: qsTr("  模型")
                    contentItem: Text {
                        text: drawerModel.text
                        font: drawerModel.font
                        opacity: enabled ? 1.0 : 0.3
                        color: "black"
                        horizontalAlignment: Text.AlignLeft
                        verticalAlignment: Text.AlignVCenter
                        elide: Text.ElideRight
                    }
                    background: Rectangle {
                        color: drawerModel.hovered ?"gray":"aliceblue"
                        anchors.fill:drawerModel
                        Text {
                            text: "▶   "
                            font: drawerModel.font
                            anchors.right: parent.right
                            height: drawerModel.height
                            verticalAlignment: Text.AlignVCenter
                        }
                    }
                    onClicked: menuModel.open()
                    Menu{
                        id: menuModel
                        title:"dataset"
                        x: drawerModel.width
                        y: drawerModel.top
                        MenuItem{
                            text: qsTr("我创建的模型")
                            width: parent.width
                            height: 80
                            onClicked: {
                                if (stackView.currentItem !== model_page )
                                {
                                    while (stackView.depth > 1) {
                                        stackView.pop()
                                    }
                                    stackView.push(model_page)

                                }
                                model_page.m_mode = 0
                                model_page.updateContent()
//                                if ( updateModel() === 0 )
//                                {
//                                    tip.showmsg("获取模型成功")
//                                }
                                drawer.close()
                            }
                        }
                        MenuItem{
                            text: qsTr("被授权的模型")
                            width: parent.width
                            height: 80
                            onClicked: {
                                if (stackView.currentItem !== model_page )
                                {
                                    while (stackView.depth > 1) {
                                        stackView.pop()
                                    }
                                    stackView.push(model_page)

                                }
                                model_page.m_mode = 1
                                model_page.updateContent()
//                                if ( updateGrantModel() === 0 )
//                                {
//                                    tip.showmsg("获取模型成功")
//                                }
                                drawer.close()
                            }
                        }
                        MenuItem{
                            text: qsTr("公共模型")
                            width: parent.width
                            height: 80
                            onClicked: {
                                if (stackView.currentItem !== model_page )
                                {
                                    while (stackView.depth > 1) {
                                        stackView.pop()
                                    }
                                    stackView.push(model_page)

                                }
                                model_page.m_mode = 2
                                model_page.updateContent()
//                                if ( updateGrantModel() === 0 )
//                                {
//                                    tip.showmsg("获取模型成功")
//                                }
                                drawer.close()
                            }
                        }
                    }
                }
            }
            ItemDelegate {
                width: parent.width
                height: 80
                Button {
                    id: drawerTrain
                    anchors.fill: parent
                    text: qsTr("  训练任务  ")
                    contentItem: Text {
                        text: drawerTrain.text
                        font: drawerTrain.font
                        opacity: enabled ? 1.0 : 0.3
                        color: "black"
                        horizontalAlignment: Text.AlignLeft
                        verticalAlignment: Text.AlignVCenter
                        elide: Text.ElideRight
                    }
                    background: Rectangle {
                        color: drawerTrain.hovered ?"gray":"aliceblue"
                        anchors.fill: drawerTrain
                    }
                    onClicked: {
                        if (stackView.currentItem !== trainjob_page )
                        {
                            while (stackView.depth > 1) {
                                stackView.pop()
                            }
                            stackView.push(trainjob_page)

                        }
                        trainjob_page.updateContent()
                        //trainjob_page.setToken(m_token)
//                        if ( updateTrainjob() === 0 )
//                        {
//                            tip.showmsg("获取训练任务成功")
//                        }

                        //model_page.updateModel()
                        drawer.close()
                    }
                }
            }
            ItemDelegate {
                width: parent.width
                height: 80
                Button {
                    id: drawerApp
                    anchors.fill: parent
                    text: qsTr("  AI应用  ")
                    contentItem: Text {
                        text: drawerApp.text
                        font: drawerApp.font
                        opacity: enabled ? 1.0 : 0.3
                        color: "black"
                        horizontalAlignment: Text.AlignLeft
                        verticalAlignment: Text.AlignVCenter
                        elide: Text.ElideRight
                    }
                    background: Rectangle {
                        color: drawerApp.hovered ?"gray":"aliceblue"
                        anchors.fill: drawerApp
                    }

                    onClicked: {
                        if (stackView.currentItem !== application_page )
                        {
                            while (stackView.depth > 1) {
                                stackView.pop()
                            }
                            stackView.push(application_page)

                        }
                        application_page.updateContent()
                            //stackView.update()
//                        }
//                        if ( updateApplication() === 0 )
//                        {
//                            tip.showmsg("获取AI应用成功")
//                        }
                        drawer.close()
                    }
                }
            }

        }
    }

    HomeForm {
        id: home_page
    }

    StackView {
        id: stackView
        initialItem: home_page
        anchors.fill: parent
    }

    function show_error(msg)
    {
        tip.showmsg(msg)
    }
}
